package java8.time;

/**
 * @author
 * @date 2021年11月21日 17:06
 * @description
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class J8LocalDatetime {
    public void getCurrentDate(){
        LocalDate now = LocalDate.now();
        System.out.println("this is now date  "+now);


        Date date=new Date();
        System.out.println(date);
    }


    public void getDetailDate(){
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        int dayOfMonth = today.getDayOfMonth();
        System.out.printf("year: %d Month: %d Day: %d t",year,month,dayOfMonth);
    }


    public void handleSpecilDate(){
        LocalDate dateOfBrith = LocalDate.of(2018, 10, 15);
        System.out.println("this specil date is "+ dateOfBrith);
    }


    public void compareDate(){
        LocalDate today = LocalDate.now();
        LocalDate date1 = LocalDate.of(2021, 5, 14);
        if(today.equals(date1)){
            System.out.printf("today %s and date1 %s  are some date %n",today,date1);
        }
    }


    public void cycleDate(){
        LocalDate today = LocalDate.now();
        LocalDate dateOfBirth = LocalDate.of(2021, 5, 14);
        MonthDay birthDay = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);
        if(currentMonthDay.equals(birthDay)){
            System.out.println("today is birth day");
        }else {
            System.out.println("sorry, today is not birth day");
        }
    }


    public void getCurrentTime(){
        LocalTime now = LocalTime.now();
        System.out.println("local time now "+now);
    }


    public void plusHours(){
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusHours(2);
        System.out.println("Time after 2 hours  "+newTime);
    }


    public void nextWeek(){
        LocalDate today = LocalDate.now();
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("today is "+today);
        System.out.println("today is after 1 week  "+nextWeek);
    }


    public void minusDate(){
        LocalDate today = LocalDate.now();
        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("Date before 1 year "+previousYear);

        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("Date after 1 year "+nextYear);
    }

    public void clock(){

        Clock clock = Clock.systemUTC();
        System.out.println("clock "+clock);


        Clock defaultZone = Clock.systemDefaultZone();
        System.out.println("clock "+defaultZone);
    }


    public void isBeforeOrIsAfter(){
        LocalDate today = LocalDate.now();

        LocalDate tomorrow = LocalDate.of(2018, 1, 19);
        if(tomorrow.isAfter(today)){
            System.out.println("tomorrow comes after today");
        }


        LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
        if(yesterday.isBefore(today)){
            System.out.println("yesterday is day before today");
        }
    }


    public void getZoneTime(){

        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime localtDateAndTime  = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork   = ZonedDateTime.of(localtDateAndTime, america);
        System.out.println("现在的日期和时间在特定的时区:"+dateAndTimeInNewYork  );
    }


    public void checkCardExpiry(){
        YearMonth now = YearMonth.now();
        System.out.printf("Days in month year %s: %d %n",now,now.lengthOfMonth());

        YearMonth creditCardExpiry  = YearMonth.of(2028, Month.FEBRUARY);
        System.out.printf("Your credit card expires on %s %n",creditCardExpiry );
    }


    public void isLeapYear(){
        LocalDate today = LocalDate.now();
        if (today.isLeapYear()){
            System.out.println("this year is leap year");
        }else {
            System.out.println("2021 is a not leap year");
        }
    }


    public void calcDateDays(){
        LocalDate now = LocalDate.now();
        LocalDate java8Reliase = LocalDate.of(2018, Month.MAY, 14);
        Period periodToNextJavaRelease = Period.between(now, java8Reliase);
        System.out.println("Months left between today and Java 8 release : "
                + periodToNextJavaRelease.getMonths() );
    }

    public void zoneOffset(){
        LocalDateTime dateTime = LocalDateTime.of(2018, Month.FEBRUARY, 14, 19, 30);
        ZoneOffset of = ZoneOffset.of("+05:30");
        OffsetDateTime offsetDateTime = OffsetDateTime.of(dateTime, of);
        System.out.println("date and time  with timezone offset in  java :"+offsetDateTime);

    }

    public void getTimestamp(){
        Instant timestamp = Instant.now();
        System.out.println("what is value  of this  instant "+timestamp);
    }


    public void formateDate(){
        String dayAfterTommorrow="20210512";
        LocalDate parse = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.printf("Date generated from String %s is %s %n",dayAfterTommorrow,parse);
    }
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {


        for (Method method : J8LocalDatetime.class.getDeclaredMethods()) {

            if (!method.getName().equals("main")){
                System.out.print(method.getName()+": ");
                method.invoke(new J8LocalDatetime(), null);
            }
        }

    }
}
